//
//  FSCalendarScopeViewController.m
//  THCalendar
//
//  Created by 魏鑫 on 2020/12/30.
//

#import "FSCalendarScopeViewController.h"
#import "THCalendar.h"
#import "THCalendarCell.h"

#import "zhPopupController.h"
#import "THCalendarPopView.h"

#import "THCalendarPushView.h"

@interface FSCalendarScopeViewController () <UITableViewDataSource,UITableViewDelegate,FSCalendarDataSource,FSCalendarDelegate,UIGestureRecognizerDelegate>

@property (strong,nonatomic) THCalendar *calendar;

@property (strong, nonatomic) NSArray<NSString *> *datesWithEvent;
@property (strong, nonatomic) NSDateFormatter *eventDateFormatter;
@property (strong, nonatomic) NSCalendar *gregorian;

@end

@implementation FSCalendarScopeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.calendar];
    [self calendarConfig];
    
    [self testButton];
}

- (void)calendarConfig
{
    self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    
    NSLocale *chinese = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
    
    self.eventDateFormatter = [[NSDateFormatter alloc] init];
    self.eventDateFormatter.locale = chinese;
    self.eventDateFormatter.dateFormat = @"yyyy-MM-dd";
    
    self.datesWithEvent = @[@"2020-01-03",
                            @"2020-01-05",
                            @"2020-01-11",
                            @"2020-01-18"];
    
    [self.calendar selectDate:[NSDate dateWithTimeIntervalSinceNow:60*60*24*1]];
}

#pragma mark - FSCalendarDataSource
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
    return [NSDate date];
}

- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
    return [NSDate dateWithTimeIntervalSinceNow:3600*24*99];
}

- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date
{
    return [self.calendar.lunarFormatter stringFromDate:date];
}

- (__kindof FSCalendarCell *)calendar:(FSCalendar *)calendar cellForDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)position
{
    THCalendarCell *cell = [calendar dequeueReusableCellWithIdentifier:@"cell" forDate:date atMonthPosition:position];
    return cell;
}

- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date
{
    if ([self.datesWithEvent containsObject:[self.eventDateFormatter stringFromDate:date]]) {
        return 1;
    } else {
        return 0;
    }
}

#pragma mark - FSCalendarDelegate
 
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    //点击上一页与下一页自动滚动
    if (monthPosition == FSCalendarMonthPositionNext || monthPosition == FSCalendarMonthPositionPrevious) {
        [calendar setCurrentPage:date animated:YES];
    }
}

- (void)calendar:(FSCalendar *)calendar willDisplayCell:(FSCalendarCell *)cell forDate:(NSDate *)date atMonthPosition: (FSCalendarMonthPosition)monthPosition
{
    [self configureCell:cell forDate:date atMonthPosition:monthPosition];
}

#pragma mark - Private methods
- (void)configureCell:(FSCalendarCell *)cell forDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    
    THCalendarCell *diyCell = (THCalendarCell *)cell;
    
    // Custom today circle
    diyCell.circleImageView.hidden = ![self.gregorian isDateInToday:date];
    
    // Configure selection layer
    if (monthPosition == FSCalendarMonthPositionCurrent) {
        diyCell.selectionLayer.hidden = NO;
    } else {
        diyCell.circleImageView.hidden = YES;
        diyCell.selectionLayer.hidden = YES;
    }
}

#pragma mark - Lazyload

- (THCalendar *)calendar
{
    if (!_calendar) {
        CGFloat top = [UIApplication sharedApplication].statusBarFrame.size.height + self.navigationController.navigationBar.frame.size.height;
        
        _calendar = [[THCalendar alloc] initWithFrame:CGRectMake(0, top, CGRectGetWidth(self.view.frame), 400)];
        _calendar.delegate = self;
        _calendar.dataSource = self;
        [_calendar registerClass:[THCalendarCell class] forCellReuseIdentifier:@"cell"];
    }
    return _calendar;
}

@end


@implementation FSCalendarScopeViewController (Push)

- (void)testButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"弹出日历" forState:UIControlStateNormal];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor orangeColor];
    button.frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 180, self.view.frame.size.width, 50);
    [button addTarget:self action:@selector(testButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setTitle:@"弹出日历2" forState:UIControlStateNormal];
    [self.view addSubview:button2];
    button2.backgroundColor = [UIColor orangeColor];
    button2.frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 120, self.view.frame.size.width, 50);
    [button2 addTarget:self action:@selector(testButtonClick2:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)testButtonClick:(id)sender
{
    THCalendarPopView * popView = [[THCalendarPopView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), THCalendarPopHeight)];
    popView.titleLabel.text = @"请设置出发时间";
    
    zhPopupController *popController = [[zhPopupController alloc] initWithView:popView size:popView.bounds.size];
    popController.maskType = zhPopupMaskTypeDarkBlur;
    popController.layoutType = zhPopupLayoutTypeBottom;
    popController.presentationStyle = zhPopupSlideStyleFromBottom;
    [popController showInView:self.view completion:^{
        
    }];
}

- (void)testButtonClick2:(id)sender
{
    THCalendarPushView *pushView = [[THCalendarPushView alloc] init];
    pushView.title = @"请设置出发时间";
//    pushView.contentHeight = 500;
    [pushView show];
}

@end
